<script>

var EXEC = {
	tate : 0,
	yoko : 0
};

var SELF = '/test-env/20121221-WindowPolicy/policy-test.html';
var HOST_A = 'localhost';
var HOST_B = '127.0.0.1';

/*
var SELF = '/camouflageblog/20121228/policy-test.html';
var HOST_A = 'www28.atpages.jp';
var HOST_B = '49.212.30.56';
*/

function makeUrl(in_tate, in_yoko, in_params)
{
	var url = 'http://' + gWin.conf[in_tate][in_yoko].host + SELF;
	var query = '_self=' + gWin.ix2name(in_tate, in_yoko);
	for (var field in in_params) {
		query += '&' + field + '=' + in_params[field];
	}
	return url + '?' + query;
}

function openLeft(in_name, in_params)
{
	var ix = gWin.name2ix(in_name);
	var url = makeUrl(ix.tate, ix.yoko + 1, in_params);
	window.open(url, gWin.ix2name(ix.tate, ix.yoko + 1));
}

function openDown(in_name, in_params)
{
	var ix = gWin.name2ix(in_name);
	var url = makeUrl(ix.tate + 1, ix.yoko, in_params);
	var iframe = document.createElement('IFRAME');
	document.documentElement.appendChild(iframe);
	iframe.src = url;
	iframe.style.width = '80%';
	iframe.style.height = '50%';
	iframe.style.border = 'solid 1px black'
	iframe.contentWindow.name = gWin.ix2name(ix.tate + 1, ix.yoko);
}

function makeTestLink(in_params)
{
	var table = document.createElement('TABLE');
	for (var i = 0; i < gWin.conf.length; i++) {
		var tr = document.createElement('TR');
		for (var j = 0; j < gWin.conf[i].length; j++) {
			if ((i == EXEC.tate) && (j == EXEC.yoko)) {
				var cell = document.createElement('SPAN');
				cell.style.backgroundColor = 'yellow';
			} else {
				var cell = document.createElement('A');
				cell.href = makeUrl(i, j, in_params) + '#ADDED';
				cell.target = gWin.ix2name(i, j);
			}
			cell.appendChild(document.createTextNode('(' + i + ', ' + j + ')'));
			var td = document.createElement('TD');
			td.appendChild(cell);
			td.style.border = 'solid 1px black';
			tr.appendChild(td);
		}
		table.appendChild(tr);
	}
	table.style.borderCollapse = 'collapse';
	table.style.border = 'solid 1px black';
	table.style.margin = '1em';
	document.documentElement.appendChild(table);
}

var gWin = {
	conf : [
		[
			{
				host : HOST_A,
				open : [openLeft, openDown]
			},
			{
				host : HOST_A,
				open : [openLeft, openDown]
			},
			{
				host : HOST_B,
				open : [openLeft, openDown]
			},
			{
				host : HOST_A,
				open : [openLeft, openDown]
			},
			{
				host : HOST_A,
				open : [openDown]
			},
		],
		[
			{
				host : HOST_A,
				open : [openDown]
			},
			{
				host : HOST_A,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_A,
				open : [openDown]
			},
			{
				host : HOST_A,
				open : [openDown]
			},
		],
		[
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			}
		],
		[
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			},
			{
				host : HOST_B,
				open : [openDown]
			}
		],
		[
			{
				host : HOST_B,
				open : []
			},
			{
				host : HOST_B,
				open : []
			},
			{
				host : HOST_B,
				open : []
			},
			{
				host : HOST_B,
				open : []
			},
			{
				host : HOST_B,
				open : []
			}
		]
	],
	getConf : function(in_name) {
		var ix = this.name2ix(in_name);
		if (ix) {
			return this.conf[ix.tate][ix.yoko];
		}
		return null;
	},
	namePrefix : 'test',
	ix2name : function(in_tate, in_yoko) {
		return this.namePrefix + '_' + in_tate + '_' + in_yoko;
	},
	name2ix : function(in_name) {
		var e = in_name.split('_');
		if (e.length != 3) {
			return null;
		}
		return {
			tate : Number(e[1]),
			yoko : Number(e[2])
		};
	}
};

function getParam(in_url)
{
	if (in_url.indexOf('?') == -1) {
		return null;
	}
	var ret = {};
	var params = in_url.split('?')[1].split('&');
	for (var i = 0; i < params.length; i++) {
		var fv = params[i].split('=');
		ret[fv[0]] = fv[1];
	}
	return ret;
}

var params = getParam(location.href);
if (!params) {
	params = {
		_exec : gWin.ix2name(EXEC.tate, EXEC.yoko),
		_rand : Math.random()
	};
	location.href = makeUrl(0, 0, params);
}

var _self = params._self;
delete params._self;
var func = gWin.getConf(_self).open;
for (var i = 0; i < func.length; i++) {
	(func[i])(_self, params);
}

if (_self == params._exec) {
	makeTestLink(params);
} else {
	window.setInterval(function() {
		var b = document.body;
		if (location.href.indexOf(HOST_A) > 0) {
			var color = '#ccffcc';
		} else if (location.href.indexOf(HOST_B) > 0) {
			var color = '#ccccff';
		}
		if (location.hash) {
			color = 'red';
		}
		b.style.backgroundColor = color;
		if (b.getElementsByTagName('SPAN').length == 0) {
			if (!window.name) {
				window.name = gWin.ix2name(0, 0);
			}
			var ix = gWin.name2ix(window.name);
			var span = document.createElement('SPAN');
			span.appendChild(document.createTextNode('(' + ix.tate + ', ' + ix.yoko + ')'));
			b.appendChild(span);
		}
	}, 1000);
}


</script>
